/**
 * 基于Zepto的焦点图
 * 可定义是否自动，间隔时间，速度
 * 2015-11-5 by zdy(intval@163.com)
 */
;(function($){

	$.fn.touchSlide = function(arr){

		// 默认参数
		var opt = $.extend({

			timer: 3000,
			speed: 0.2,
			zon: 'z-on',
			scale: 0.5,
			auto: true

		}, arr || {});

		// 返回this，维持链式操作
		return this.each(function(){

			// 预缓存DOM对象
			var oF = $(this),
				oB = document.getElementById(oF.attr('id')).getElementsByTagName('ul')[0],
				oFul = oF.find('ul'), 
				oFli = oFul.children('li'), 
				oFn = oFli.length, iK = 0, iNt = 0;

			var oWd = oF.width(), oHt = Math.floor(oWd * opt.scale);

			// 
			oF.height(Math.floor(oWd * opt.scale));
			oFul.css({width:(oWd * oFn)});
			oFli.css({width:oWd});

			var btn = '<p class="f-cb">';
			for(var i = 0; i < oFn; i++){
				btn += '<i'+ (i == 0 ? ' class="'+ opt.zon +'"' : '') +'>&nbsp;</i>';
			}
			btn += '</p>';
			oF.append(btn);

			// is auto
			if(opt.auto){
				iNt = setInterval(function(){ slideDo(); }, opt.timer);
			}

			// left && right
			oFul.on('swipeLeft', function(){
				if(iK >= (oFn - 1)) return !1;
				slideDo(); 
				clearInterval(iNt);
			});
			oFul.on('swipeRight', function(){ 
				if(iK <= 0) return !1;
				slideDo(1); 
				clearInterval(iNt);
			});
							
			function resetIdx(type){
				if(type == 0){ iK++; iK = iK >= oFn ? 0 : iK; }else{ iK--; iK = iK <= -1 ? oFn - 1 : iK; }
			}

			function slideDo(){
				var type = arguments[0] || 0;
				resetIdx(type);
				var _kernel = kernel();
				oB.style.cssText = 'width:'+ (oWd * oFn) +'px;'+ _kernel + 'transition:all '+ opt.speed +'s linear;'+ _kernel +'transform:translate3d('+(-iK*oF.width())+'px, 0px, 0px);';
				oFul.next().find('i').removeClass(opt.zon).eq(iK).addClass(opt.zon);
			}

			function kernel(){
				var _style = document.createElement('div').style;
		        var _kernel = ['-webkit-', '-moz-', '-o-', '-ms-'];
		        for(var i in _kernel){
		            var _v = _kernel[i] + 'transform';
		            if(_style[_v] !== undefined) return _kernel[i];
		        }
		        return '';
			}
		});
	};

})(Zepto);